package wili.b1;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Matcher;

import wili.c.Ca;
import wili.c.File9a;
import wili.c.ReplaceReg9;

/**
 * @2022-08-31
 */
public class InsertIncNumToFile9a extends InsertIncNumToFile9 {
	static String chgList = "c1815492";// "ch1814400";

	public static List<File> getPropertiesFiles() {
		List<File> ret = new ArrayList<>();
		List<File> dirs = new ArrayList<>();
		{//in java
			add(1, dirs, "C:\\Perforce\\" + chgList
					+ "\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\java\\tcli\\tcli-resources\\target\\classes");
			add(1, dirs, "C:\\Perforce\\" + chgList
					+ "\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\java\\anly\\meta\\src\\main\\resources\\meta\\globalization");
		}
		{//in node
			add(1, dirs, "C:/Perforce/" + chgList + "/BUSMB_B1/SBO/dev/Source/WebClient/node/client/src");
		}
		for (File dir : dirs) {
			File9a.findFiles_byPredi(9999, dir, ret, InsertIncNumToFile9a.Predicates.propFile,
					InsertIncNumToFile9a.Predicates.skipDir);
		}
		return ret;
	}

	static void add(int opt, List<File> toDirs, String path) {
		if (opt < 0)
			return;
		File file = new File(path);
		toDirs.add(file);
	}

	public static List<File> getJsonFiles() {
		List<File> ret = new ArrayList<>();
		{
			List<File> dirs = new ArrayList<>();
			{//in java
				add(1, dirs, "C:\\Perforce\\" + chgList
						+ "\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\java\\tcli\\tcli-resources\\target\\classes");
				add(1, dirs, "C:\\Perforce\\" + chgList
						+ "\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\java\\anly\\meta\\src\\main\\resources\\meta\\globalization");
				add(1, dirs, "C:\\Perforce\\" + chgList
						+ "\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\java\\anly\\meta\\src\\main\\resources\\meta\\json");
			}
			{//in node
				add(1, dirs, "C:/Perforce/" + chgList + "/BUSMB_B1/SBO/dev/Source/WebClient/node/client/src");
			}
			//
			for (File dir : dirs) {
				File9a.findFiles_byPredi(9999, dir, ret, InsertIncNumToFile9a.Predicates.jsonFile,
						InsertIncNumToFile9a.Predicates.skipDir);
			}
		}
		return ret;
	}

	public static interface Predicates {
		Predicate<File> jsonFile = new Predicate<File>() {
			@Override
			public boolean test(File file) {
				String name = file.getName();
				if (name.endsWith(".json"))
					return true;
				return false;
			}
		};
		Predicate<File> propFile = new Predicate<File>() {
			@Override
			public boolean test(File file) {
				String name = file.getName();
				if (name.endsWith(".properties")) {
					return true;
				}
				return false;
			}
		};
		Predicate<File> skipDir = new Predicate<File>() {
			final Set<File> skipSet = new HashSet<>(Arrays.asList(new File("C:\\Perforce\\" + chgList
					+ "\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\node\\client\\src\\resources\\sap")));

			@Override
			public boolean test(File file) {
				String name = file.getName();
				if (name.equals("VOIGNP.detailView.json"))
					Ca.pause();
				if (name.equals("test") || name.equals("example") || name.equals("of365") || name.equals("share"))
					return true;
				if (skipSet.contains(file)) {
					return true;
				}
				return false;
			}
		};
	}

	/**
	 * 正则替换后执行MARK替换。
	 */
	static String replaceReg_withIncNum(File file, String fileSt, String regexp, String replace) {
		//eg. "([^=#\r\n]+)=([^=\r\n]+)", "$1=$2;`;`",";`;")
		Predicate<Matcher> testMatcherBeforeReplace = new Predicate<Matcher>() {
			@Override
			public boolean test(Matcher matcher) {
				if ("456".isEmpty())
					return true;
				String group = matcher.group();
				if (group.contains("Reset")) {
					return true;
				}
				return false;
			}
		};
		return ReplaceReg9.replaceReg_withIncNum(fileSt, regexp, replace, MARK, INC, testMatcherBeforeReplace);
	}

	static boolean acceptFileSufix(String fname) {
		if (fname.endsWith(".json") || fname.endsWith(".properties") || fname.endsWith(".xml"))
			return true;
		//不可插入 js, java file,危险！
		return false;
	}

	/**
	 * 插入自增数字到文件。
	 */
	static boolean insertIncNum_to_file(boolean insert, File file, String reg, String replace) {
		//安全起见，rollback 模式中必须含有MARK，MARK在插入前的文件中不可能存在。
		Ca.asert(file.isFile(), file);
		String fileSt;
		String fname = file.getName();
		{//只处理给定后缀的文件 & 不处理含有MARK的文件。
			if (!acceptFileSufix(fname))
				return false;
			File[] files;
			fileSt = File9a.strOfFile(file, "");
			if (fileSt.contains(MARK)) {
				if ("asdf".isEmpty())
					throw new RuntimeException((file.getName() + " contains " + MARK));
				return false;
			}
		}
		String retSt = fileSt;
		if (insert) {
			int times = INC[0];
			//grid-col-freight3-distribution-rule = Freight 3 Distribution Rule
			retSt = replaceReg_withIncNum(file, fileSt, reg, replace);
			if (retSt == null)
				return false;
			if (times == INC[0]) {
				Ca.log(1, " not added " + file.getName());
				return false;
			} else {
			}
		} else {//roolback.
			retSt = retSt.replaceAll(r(";\\d+;`"), "");
		}
		if (retSt.equals(fileSt)) {
			Ca.log(1, " not rollback " + file.getName());
			return false;
		} else {
		}
		File9a.str2file(retSt, file, file.lastModified());
		if (insert) {
			Ca.log(1, " added " + file.getName());
		} else {
			Ca.log(1, " rollbacked " + file.getName());
		}
		return true;
	}
}
